<?php
class PDF
{
	static public function make($loan,$allot,$member,$protocal_con,$dest='D'){
$member['sid'] = $member['id'];
$member['id'] =  str_pad($member['id'],8,'0',STR_PAD_LEFT);
$bindinfo = $member['bindinfo'];
if(empty($bindinfo['names']))
{
	$bindinfo['names'] = '--';
	$bindinfo['certiNumber'] = '--';
}
Vendor('tcpdf.qianduoduo');
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('钱多多');
$pdf->SetTitle($protocal_con['title']);
$pdf->SetSubject($protocal_con['title']);
$pdf->SetKeywords($protocal_con['title']);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('stsongstdlight','',12);
$html = <<<EOD
<style>
*{margin:0;padding:0;}
body{font-family: Arial,'宋体';font-size: 12px;}
p.lh{line-height:3px;text-indent:1pt}
p.h1{font-size:24px;}
p.bold{font-weight:bold;}
span.s,span.l{display:block;height:15px;}
</style>
<p align="center" class="bold h1">借 款 协 议</p>
<p align="right">协议编号：{$loan['loansn']}-{$loan['tid']}</p>
<p align="center" class="bold">第一部分：借款信息及相关明细</p>
<p class="lh">
<table border="1" cellspacing="0" cellpadding="0" align="center;">
  <tr>
    <td width="71" rowspan="2">出借人</td>
    <td width="119" align="right">姓名：</td>
    <td width="169" align="left">&nbsp;&nbsp; {$bindinfo['names']} <span style="color:#fff;">　&nbsp;　&nbsp;　&nbsp;　^$</span></td>
    <td width="93" align="right">身份证号：</td>
    <td width="177" align="left">&nbsp;&nbsp; {$bindinfo['certiNumber']} </td>
  </tr>
  <tr>
    <td width="119" align="right">钱多多用户名：</td>
    <td width="169" align="left">&nbsp;&nbsp; {$member['username']}</td>
    <td width="93" align="right">钱多多ID：</td>
    <td width="177" align="left">&nbsp;&nbsp; {$member['id']}</td>
  </tr>
  <tr>
    <td width="71" rowspan="2">借款人</td>
    <td width="119" align="right">姓名：</td>
    <td width="169" align="left">&nbsp;&nbsp;{$loan['username']}</td>
    <td width="93" align="right">身份证号：</td>
    <td width="177" align="left">&nbsp;&nbsp;{$loan['cardnum']}</td>
  </tr>
  <tr>
    <td width="119" align="right">质权代码：</td>
    <td width="169" align="left">&nbsp;&nbsp;{$loan['cateid']}</td>
    <td width="93" align="right">质权ID：</td>
    <td width="177" align="left">&nbsp;&nbsp;{$loan['pid']}</td>
  </tr>
  <tr>
    <td width="71">服务方</td>
	<td width="288" align="left" colspan="2">&nbsp;&nbsp;上海旭胜金融信息服务有限公司</td>
    <td width="93" align="right">服务费率：</td>
    <td width="177" align="left">&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td width="71" rowspan="4">出借明细</td>
    <td width="119" align="right">出借本金：</td>
    <td width="169" align="left">&nbsp;&nbsp;人民币{$loan['tmoney']}元</td>
    <td width="93">出借利率：</td>
    <td width="177" align="left">&nbsp;&nbsp;{$loan['yearrate']}%</td>
  </tr>
  <tr>
    <td width="119" align="right">借款出借日期：</td>
    <td width="169" align="left">&nbsp;&nbsp;{$loan['ttime']}</td>
    <td width="93">起息日期：</td>
    <td width="177" align="left">&nbsp;&nbsp;{$loan['lssuingtime']}</td>
  </tr>
  <tr>
    <td width="119" align="right">借款到期日期：</td>
    <td width="169" align="left">&nbsp;&nbsp;{$loan['endtime']}</td>
    <td width="93">还款期数：</td>
    <td width="177" align="left">&nbsp;&nbsp;{$loan['loanmonth']}个月(详见附件一)</td>
  </tr>
  <tr>
    <td width="119" align="right">还款方式：</td>
    <td width="169" align="left">&nbsp;&nbsp;{$loan['payfunction']}</td>
    <td width="93">借款用途：</td>
    <td width="177" align="left">&nbsp;&nbsp;</td>
  </tr>
  <tr>
    <td width="71">借款描述</td>
    <td width="557" colspan="4" align="left">&nbsp;&nbsp;{$loan['title']}</td>
  </tr>
</table>
</p>
EOD;
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo.png', 90,110, 50);
//通用主体内容
$html = str_replace(array('{amount}'),array($loan['tendermoney']),$protocal_con['content']);

$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo-2.png', 110, 180, 50);
$html ='<p class="lh"><strong>附件一：还款明细</strong><br /></p>';
$html.= '<table  border="1" cellspacing="0" cellpadding="0" align="center;">';
$html.= '<tr><td width="50">编号</td><td width="120">还款日期</td><td width="120">偿还本息（元）</td>';
$html.= '<td width="120">偿还本金（元）</td><td width="120">偿还利息（元）</td><td width="120">剩余本金（元）</td></tr>';
foreach($allot as $key=>$val)
{
	$html .="<tr><td>".($key+1)."</td><td>{$val['endtime']}</td><td>{$val['money']}</td><td>{$val['tendermoney']}</td>";
	$html .="<td>{$val['ratemoney']}</td><td>{$val['residuemoney']}</td></tr>";
}
$html .='</table></p><p><strong>以下无内容</strong></p>';
$rand = rand(10,99);
$filename = "qianduoduo-{$loan['id']}-{$member['sid']}-{$loan['tmoney']}-{$rand}.pdf";
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo-3.png', 140, 20, 50);
$pdf->lastPage();
$pdf->Output($filename, $dest);
return $filename;
	}



	static public function makenew($loan,$allot,$member,$protocal_con,$dest='D'){
$domainRoot = 'http://'.DOMAIN_URI;
$member['sid'] = $member['id'];
$member['id'] =  str_pad($member['id'],8,'0',STR_PAD_LEFT);
$bindinfo = $member['bindinfo'];
if(empty($bindinfo['names']))
{
	$bindinfo['names'] = '--';
	$bindinfo['certiNumber'] = '--';
}
Vendor('tcpdf.qianduoduo');
$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('钱多多');
$pdf->SetTitle($protocal_con['title']);
$pdf->SetSubject($protocal_con['title']);
$pdf->SetKeywords($protocal_con['title']);
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetFont('stsongstdlight','',12);
$html = <<<EOD
<style>
*{margin:0;padding:0;}
body{font-family: Arial,'宋体';font-size: 12px;}
p.lh{line-height:3px;text-indent:1pt}
p.h1{font-size:24px;}
p.bold{font-weight:bold;}
span.s,span.l{display:block;height:15px;}
</style>
<p align="center" class="bold h1">借款、居间及专项委托协议</p>
<p align="right">协议编号：{$loan['loansn']}-{$loan['tid']}</p>
<p align="center" class="bold">第一部分：借款协议部分</p>
<p class="lh">
<strong>一、出借款项概况</strong><br/>
<strong>1.1 借款信息及相关明细</strong><br/>
<table border="1" cellspacing="0" cellpadding="0" align="center;">
  <tr>
    <td width="71" rowspan="2">出借人</td>
    <td width="119" align="right">姓名：</td>
    <td width="169" align="left">&nbsp;{$bindinfo['names']} <span style="color:#fff;">　&nbsp;　&nbsp;　&nbsp;　^$</span></td>
    <td width="93" align="right">身份证号：</td>
    <td width="177" align="left">&nbsp;{$bindinfo['certiNumber']} </td>
  </tr>
  <tr>
    <td width="119" align="right">钱多多用户名：</td>
    <td width="169" align="left">&nbsp;{$member['username']}</td>
    <td width="93" align="right">钱多多ID：</td>
    <td width="177" align="left">&nbsp;{$member['id']}</td>
  </tr>
  <tr>
    <td width="71">借款人</td>
    <td width="557" colspan="4" align="left">&nbsp;编号：{$loan['borrowId']}<br/>&nbsp;为了保护借款人隐私，可以凭借款人编号在以下地址查询借款人信息<br/>&nbsp;{$domainRoot}/borrowinfo.html</td>
  </tr>
  <tr>
    <td width="71">居间方</td>
	<td width="288" align="left" colspan="2">&nbsp;上海旭胜金融信息服务股份有限公司</td>
    <td width="93" align="right">服务费率：</td>
    <td width="177" align="left">&nbsp;对出借人免居间服务费</td>
  </tr>
  <tr>
    <td width="71" rowspan="3">出借明细</td>
    <td width="119" align="right">出借本金：</td>
    <td width="169" align="left">&nbsp;人民币{$loan['tmoney']}元</td>
    <td width="93">出借利率：</td>
    <td width="177" align="left">&nbsp;年化{$loan['yearrate']}%</td>
  </tr>
  <tr>
    <td width="119" align="right">借款出借日期：</td>
    <td width="169" align="left">&nbsp;{$loan['ttime']}</td>
    <td width="93">起息日期：</td>
    <td width="177" align="left">&nbsp;{$loan['lssuingtime']}</td>
  </tr>
  <tr>
    <td width="119" align="right">借款到期日期：</td>
    <td width="169" align="left">&nbsp;{$loan['endtime']}</td>
    <td width="93">还款期数：</td>
    <td width="177" align="left">&nbsp;{$loan['loanmonth']}个月(详见附件一)</td>
  </tr>
  <tr>
    <td width="71">借款描述</td>
    <td width="557" colspan="4" align="left">&nbsp;{$loan['title']}</td>
  </tr>
</table>
</p>
EOD;
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo-4.png', 140,110, 50);
//通用主体内容
$html = str_replace('{username}',$loan['username'],$protocal_con['content']);
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo-5.png', 140, 200, 50);
$html ='<p class="lh"><strong>附件一：还款明细</strong><br /></p>';
$html.= '<table  border="1" cellspacing="0" cellpadding="0" align="center;">';
$html.= '<tr><td width="50">编号</td><td width="120">还款日期</td><td width="120">偿还本息（元）</td>';
$html.= '<td width="120">偿还本金（元）</td><td width="120">偿还利息（元）</td><td width="120">剩余本金（元）</td></tr>';
foreach($allot as $key=>$val)
{
	$html .="<tr><td>".($key+1)."</td><td>{$val['endtime']}</td><td>{$val['money']}</td><td>{$val['tendermoney']}</td>";
	$html .="<td>{$val['ratemoney']}</td><td>{$val['residuemoney']}</td></tr>";
}
$html .='</table></p><p><strong>以下无内容</strong></p>';
$rand = rand(10,99);
$filename = "qianduoduo-{$loan['id']}-{$member['sid']}-{$loan['tmoney']}-{$rand}.pdf";
$pdf->AddPage();
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Image(K_PATH_IMAGES.'company-logo-6.png', 140, 20, 50);
$pdf->lastPage();
$pdf->Output($filename,$dest);
return $filename;
	}


}